package com.uls.web.controller.lh;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSONObject;
import com.uls.framework.config.MinioProp;
import com.uls.framework.utils.MinioUtils;
import com.uls.lh.domain.LhFeeStandard;
import com.uls.lh.service.ILhFeeStandardService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.uls.common.annotation.Log;
import com.uls.common.core.controller.BaseController;
import com.uls.common.core.domain.AjaxResult;
import com.uls.common.enums.BusinessType;
import com.uls.lh.domain.LhCarinfo;
import com.uls.lh.service.ILhCarinfoService;
import com.uls.common.utils.poi.ExcelUtil;
import com.uls.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 车辆信息管理Controller
 * 
 * @author lh
 * @date 2024-11-06
 */
@RestController
@RequestMapping("/lh/carinfo")
public class LhCarinfoController extends BaseController
{
    @Autowired
    private ILhCarinfoService lhCarinfoService;
    @Autowired
    private MinioUtils minioUtils;
    @Autowired
    private MinioProp minioProp;
    @Autowired
    private ILhFeeStandardService lhFeeStandardService;

    /**
     * 查询车辆信息管理列表
     */
    @PreAuthorize("@ss.hasPermi('lh:carinfo:list')")
    @GetMapping("/list")
    public TableDataInfo list(LhCarinfo lhCarinfo)
    {
        startPage();
        List<LhCarinfo> list = lhCarinfoService.selectLhCarinfoList(lhCarinfo);
        return getDataTable(list);
    }



    @GetMapping("/getAllCars")
    public AjaxResult getAllCars()
    {
        List<LhCarinfo> carList = lhCarinfoService.selectLhCarinfoList(null);
        List<LhCarinfo> carList1 = new ArrayList<>();
        if (carList != null && !carList.isEmpty()) {
            // 获取费用标准表中所有 carId
            Set<Long> feeStandardCarIds = new HashSet<>();
            List<LhFeeStandard> feeStandardList = lhFeeStandardService.selectLhFeeStandardList(null);

            // 将费用标准中的 carId 存入集合
            for (LhFeeStandard feeStandard : feeStandardList) {
                feeStandardCarIds.add(feeStandard.getCarId());
            }

            // 遍历车辆信息，找出在费用标准表中没有的 carId
            Set<Long> missingCarIds = new HashSet<>();
            for (LhCarinfo car : carList) {
                if (!feeStandardCarIds.contains(car.getCarId())) {
                    missingCarIds.add(car.getCarId());
                }
            }

            // 输出没有费用标准的车辆 carId
            if (!missingCarIds.isEmpty()) {
                System.out.println("费用标准表里没有的车牌号/车辆ID: " + missingCarIds);
                for (Long CarId:missingCarIds){
                    carList1.add(lhCarinfoService.selectLhCarinfoByCarId(CarId));

                }
            }
        }


        // 返回成功结果
        return success(carList1);
    }

    /**
     * 导出车辆信息管理列表
     */
    @PreAuthorize("@ss.hasPermi('lh:carinfo:export')")
    @Log(title = "车辆信息管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, LhCarinfo lhCarinfo)
    {
        List<LhCarinfo> list = lhCarinfoService.selectLhCarinfoList(lhCarinfo);
        ExcelUtil<LhCarinfo> util = new ExcelUtil<LhCarinfo>(LhCarinfo.class);
        util.exportExcel(response, list, "车辆信息管理数据");
    }


    /**
     * 获取所有车辆信息
     * @return
     */
    @GetMapping("/getAllCarsbystatus")
    public AjaxResult getAllCarsbystatus()
    {
        List<LhCarinfo> lhCarinfos= lhCarinfoService.selectLhCarinfobystatus(1L);
        return success(lhCarinfos);
    }
    /**
     * 获取车辆信息管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('lh:carinfo:query')")
    @GetMapping(value = "/{carId}")
    public AjaxResult getInfo(@PathVariable("carId") Long carId)
    {
        return success(lhCarinfoService.selectLhCarinfoByCarId(carId));
    }

    /**
     * 新增车辆信息管理
     */
    @PreAuthorize("@ss.hasPermi('lh:carinfo:add')")
    @Log(title = "车辆信息管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody LhCarinfo lhCarinfo)
    {
        return toAjax(lhCarinfoService.insertLhCarinfo(lhCarinfo));
    }

    /**
     * 修改车辆信息管理
     */
    @PreAuthorize("@ss.hasPermi('lh:carinfo:edit')")
    @Log(title = "车辆信息管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody LhCarinfo lhCarinfo)
    {
        return toAjax(lhCarinfoService.updateLhCarinfo(lhCarinfo));
    }

    /**
     * 删除车辆信息管理
     */
    @PreAuthorize("@ss.hasPermi('lh:carinfo:remove')")
    @Log(title = "车辆信息管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{carIds}")
    public AjaxResult remove(@PathVariable Long[] carIds)
    {
        return toAjax(lhCarinfoService.deleteLhCarinfoByCarIds(carIds));
    }

    /**
     * 上传
     *
     * @param file
     * @param request
     * @return
     */
    @PostMapping("/uploadMinio")
    public String uploadMinio(@RequestParam(name = "file", required = false) MultipartFile file, HttpServletRequest request) {
        JSONObject res = null;
        try {
            res = minioUtils.uploadFile(file, minioProp.getBucket());
        } catch (Exception e) {
            e.printStackTrace();
            res.put("code", 0);
            res.put("msg", "上传失败");
        }
        return res.toJSONString();
    }

    @PreAuthorize("@ss.hasPermi('lh:carinfo:down')")
    @PostMapping("/imageDown")
    public  void imageDown(String fileName, HttpServletResponse response){
        minioUtils.download(minioProp.getBucket(),fileName,response);
    }
}
